package com.ht.common.core.convert;

import cn.hutool.core.bean.BeanUtil;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Description 相同属性不同类之间的对象转换属性拷贝
 * @Version 1.0
 * @Author zmx
 * @Date 2025/3/27 0027 15:02
 */
public class EntityDtoConverter {

    /**
     * 类转场，先用这个吧，后续有优秀想法再实现自己的
     * @param sourceObject
     * @param tClass
     * @param ignoreProperties
     * @param <T>
     * @return
     */
    public static <T> T convert(Object sourceObject, Class<T> tClass, String... ignoreProperties){
        if(null==sourceObject){
            return null;
        }
        return BeanUtil.copyProperties(sourceObject,tClass,ignoreProperties);
    }

    /**
     * 转换List
     * @param sourceList
     * @param tClass
     * @param ignoreProperties
     * @param <T>
     * @return
     */
    public static <T> List<T> convertList(Collection<Object> sourceList, Class<T> tClass, String... ignoreProperties){
        List<T> listDto= Optional.ofNullable(sourceList).orElseGet(Collections::emptyList).stream().map((item)->{
            return BeanUtil.copyProperties(item,tClass,ignoreProperties);
        }).collect(Collectors.toList());
        return listDto;
    }
}
